package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.device.dto.output.InspectStandardCategoryExcelEx;
import com.be.beadmin.device.entity.InspectStandardCategory;
import com.be.beadmin.device.dto.output.InspectStandardCategoryEx;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.service.InspectStandardCategoryService;
import com.be.beadmin.device.dto.input.InspectStandardCategoryQueryCriteria;
import com.be.beadmin.device.dto.input.InspectStandardCategoryDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zhouzhou
* @date 2023-09-19
**/
@RestController
public class InspectStandardCategoryController {

    @Autowired
    private  InspectStandardCategoryService inspectStandardCategoryService;


    @PostMapping(value = "/device/getInspectStandardCategoryByPage")
    public ResultModel getInspectStandardCategoryByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectStandardCategoryService.getInspectStandardCategoryByPage(pageEntity)) ;
    }

    @PostMapping("/device/addInspectStandardCategory")
    public ResultModel addInspectStandardCategory(@RequestBody InspectStandardCategoryDto inspectStandardCategoryDto){
        inspectStandardCategoryService.addInspectStandardCategory(inspectStandardCategoryDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delInspectStandardCategory")
    public ResultModel delInspectStandardCategory(@RequestBody InspectStandardCategoryDto inspectStandardCategoryDto){
        inspectStandardCategoryService.delInspectStandardCategory(inspectStandardCategoryDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateInspectStandardCategory")
    public ResultModel updateInspectStandardCategory(@RequestBody InspectStandardCategoryDto inspectStandardCategoryDto){
        inspectStandardCategoryService.updateInspectStandardCategory(inspectStandardCategoryDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportInspectStandardCategory")
    public void exportInspectStandardCategory(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectStandardCategoryEx> inspectStandardCategoryPage = inspectStandardCategoryService.getInspectStandardCategoryByPage(pageEntity);
        List<InspectStandardCategoryEx> inspectStandardCategoryExes = inspectStandardCategoryPage.getContent();
        List<InspectStandardCategoryExcelEx> inspectStandardCategoryExcelExes=inspectStandardCategoryExes.stream().map(item->{
            InspectStandardCategoryExcelEx excel=new InspectStandardCategoryExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectStandardCategory接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), InspectStandardCategoryExcelEx.class).sheet("sheet1").doWrite(inspectStandardCategoryExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), InspectStandardCategoryController.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(inspectStandardCategoryExcelExes);
    }
}